package com.lanou.tipsy.adapter.recommend;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.recommend.hot_songs.SongListEntity;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by FengJun on 15/10/26.
 * 热门歌曲推荐适配器
 */
public class HotSongsAdapter extends BaseAdapter {
    public static final String TAG = "HotSongsAdapter";
    private ArrayList<SongListEntity> datas;
    private Context context;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;

    public HotSongsAdapter(ArrayList<SongListEntity> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_hot_songs, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (SimpleDraweeView) convertView.findViewById(R.id.draw_hot_songs);
            viewHolder.singerTv = (TextView) convertView.findViewById(R.id.tv_hot_songs_singer);
            viewHolder.songNameTv = (TextView) convertView.findViewById(R.id.tv_hot_songs_name);
            viewHolder.recommendTv = (TextView) convertView.findViewById(R.id.tv_hot_recommend);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SongListEntity entity = (SongListEntity) getItem(position);
        String url = entity.getPic_small();
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(viewHolder.imageView,
                R.mipmap.logo_2, R.mipmap.error);
        imageLoader.get(url, listener);
        viewHolder.singerTv.setText(entity.getAuthor());
        viewHolder.songNameTv.setText(entity.getTitle());
        viewHolder.recommendTv.setText(entity.getRecommend_reason());
        return convertView;
    }

    private class ViewHolder {
        private SimpleDraweeView imageView;
        private TextView singerTv;
        private TextView songNameTv;
        private TextView recommendTv;
    }
}
